package com.seari.ats;

import com.seari.utils.DataUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    //ChannelHandlerContext通道处理上下文
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws InterruptedException {
        System.out.println("======= Service ChannelActive startup ======");
        System.out.println("======= start send ======");
        int num =0;
//        while (true) {//ff 00 12 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 09 0a 06 00 00
//            String heart ="FF00050000023001";
//            //1006
//            //ff 00 12 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 0a 0a 06 00 00 一个帧 单个message
//            //ff 00 23 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 09 0a 06 00 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 09 0a 06 00 00 一个帧 多个message
//            String trainLocation ="ff 00 12 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 0a 0a 06 00 00";
//            ByteBuf in = ByteBufUtil.threadLocalDirectBuffer();
//            in.writeBytes(DataUtil.HexStrToBytes(trainLocation));
//            ctx.writeAndFlush(in);
//            num++;
//            System.out.println("send num total= "+num);
//            Thread.sleep(2000);
//        }
      while(true) {
           //// 后续帧发送  每隔两秒  连续三次
            ByteBuf in = ByteBufUtil.threadLocalDirectBuffer();
//            String trainLocationSecond = "00662133ff 00 0e 00 00 0b 10 02 01 04 b1 05 dd 00 0e 0f 01ff 00 0e 00 00 0b 10 02 01 04 b1 05 dd 00 0e 0f 01ff 00 07 00 00 04 10 03 11 01ff04010105ff10031102000105000003e9001100132000035b97057f5b97058040000000002000015b9705a95b9705c740000000001f00015b97068b5b9706a440000000001e00015b9707035b97071c40000000001d00015b97077c5b97079540000000001c00015b9707fc5b97081540000000001b00015b9708745b97088d40000000001a00015b9709155b97092e40000000001900015b97097d5b97099640000000001800015b9709ed5b970a0640000000001700015b970a755b970a9340000000001600015b970ae85b970b0640000000001500015b970b6c5b970b8a40000000001400015b970bd85b970bf140000000001300015b970c495b970c6240000000001300055b970cb55b970cb540000000001200015b970d085b970d2140000000001100035b970d635b970d6340000000001100025b970da55b970da64000000000000007d1002000121100025b970e1c5b970e1d00000000001200025b970e775b970e9000000000001300065b970edb5b970edb00000000001300025b970f265b970f3f00000000001400025b970f915b970faa00000000001500025b970ff95b97101700000000001600025b97107c5b97109a00000000001700025b9710ef5b97110d00000000001800025b9711805b97119900000000001900025b9711ec5b97120500000000001a00025b9712555b97126e00000000001b00025b9712fc5b97131500000000001c00025b9713825b97139b00000000";
//            in.writeBytes(DataUtil.HexStrToBytes(trainLocationSecond));
//            String lastLocationSecond = "00ff001200000f10010104b105dd0020000c0d060000ff0006000003100200ff001200000f10010104b105dd0020000c0d060000ff0006000003100200";
//            in.writeBytes(DataUtil.HexStrToBytes(lastLocationSecond));
           String FRAME2 = "ff 03 5d 00 " +
                  "03 5a 10 04 11 02 04 b1 00 22 " +
                  "01 00 05 5b 97 1e ba 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 05 00 00 00 00 5b 97 1f 66 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 03 5b 97 20 59 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 03 00 00 00 00 5b 97 20 5d 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 02 5b 97 20 9a 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 02 00 00 00 00 5b 97 20 c6 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "02 00 02 5b 97 21 53 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "02 00 02 00 00 00 00 5b 97 21 7a 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "03 00 02 5b 97 21 dd 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "03 00 02 00 00 00 00 5b 97 22 03 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "04 00 02 5b 97 22 91 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "04 00 02 00 00 00 00 5b 97 22 b8 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "05 00 02 5b 97 23 13 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "05 00 02 00 00 00 00 5b 97 23 39 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "06 00 02 5b 97 23 9d 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "06 00 02 00 00 00 00 5b 97 23 c5 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "07 00 02 5b 97 24 25 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "07 00 02 00 00 00 00 5b 97 24 4a 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "08 00 02 5b 97 24 b7 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "08 00 02 00 00 00 00 5b 97 24 dd 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "09 00 02 5b 97 25 3d 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "09 00 02 00 00 00 00 5b 97 25 63 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0a 00 02 5b 97 25 cb 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0a 00 02 00 00 00 00 5b 97 25 f5 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0b 00 02 5b 97 26 6d 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0b 00 02 00 00 00 00 5b 97 26 93 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0c 00 02 5b 97 27 0d 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0c 00 02 00 00 00 00 5b 97 27 34 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0d 00 02 5b 97 27 8d 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0d 00 02 00 00 00 00 5b 97 27 b2 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0e 00 02 5b 97 2b 18 00 00 00 00 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "0e 00 02 00 00 00 00 5b 97 2b 2f 00 00 0f 42 40 05 dd 00 20 00 00 00 00 00 " +
                  "01 00 02 5b 97 54 2f 00 00 00 00 00 00 0f 42 41 05 de 00 20 00 00 00 00 00 " +
                  "01 00 02 00 00 00 00 5b 97 55 96 00 00 0f 42 41 05 de 00 20 00 00 00 00 00"
                   ;
           String aa = "ff 00 12 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 0a 0a 06 00 00";
           String bb = "ff 00 05 00 00 02 30 01";
           String cc = "ff 00 23 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 09 0a 06 00 00 00 0f 10 01 01 04 b1 05 dd 00 20 00 09 0a 06 00 00";
            String thirdLocationSecond = aa + bb + cc + FRAME2+"00ff04010105ff10031102000105000003e9001100132000035ba03fff5ba0400040000000002000015ba040295ba0404740000000001f00015ba0410b5ba0412440000000001e00015ba041835ba0419c40000000001d00015ba041fc5ba0421540000000001c00015ba0427c5ba0429540000000001b00015ba042f45ba0430d40000000001a00015ba043955ba043ae40000000001900015ba043fd5ba0441640000000001800015ba0446d5ba0448640000000001700015ba044f55ba0451340000000001600015ba045685ba0458640000000001500015ba045ec5ba0460a40000000001400015ba046585ba0467140000000001300015ba046c95ba046e240000000001300055ba047355ba0473540000000001200015ba047885ba047a140000000001100035ba047e35ba047e340000000001100025ba048255ba048264000000000000007d1002000121100025ba0489c5ba0489d00000000001200025ba048f75ba0491000000000001300065ba0495b5ba0495b00000000001300025ba049a65ba049bf00000000001400025ba04a115ba04a2a00000000001500025ba04a795ba04a9700000000001600025ba04afc5ba04b1a00000000001700025ba04b6f5ba04b8d00000000001800025ba04c005ba04c1900000000001900025ba04c6c5ba04c8500000000001a00025ba04cd55ba04cee00000000001b00025ba04d7c5ba04d9500000000001c00025ba04e025ba04e1b00000000001d00025ba04e825ba04e9b00000000001e00025ba04efb5ba04f1400000000001f00025ba04f725ba04f8b00000000002000025ba0506b5ba0508900000000002000035ba050c45ba050c50000000000000003ea001100132000035ba0513b5ba0513c40000000002000015ba051655ba0518340000000001f00015ba052475ba0526040000000001e00015ba052bf5ba052d840000000001d00015ba053385ba0535140000000001c00015ba053b85ba053d140000000001b00015ba054305ba0544940000000001a00015ba054d15ba054ea40000000001900015ba055395ba0555240000000001800015ba055a95ba055c240000000001700015ba056315ba0564f40000000001600015ba056a45ba056c240000000001500015ba057285ba0574640000000001400015ba057945ba057ad40000000001300015ba058055ba0581e40000000001300055ba058715ba0587140000000001200015ba058c45ba058dd40000000001100035ba0591f5ba0591f40000000001100025ba059615ba059624000000000000007d2002000121100025ba059d85ba059d900000000001200025ba05a335ba05a4c00000000001300065ba05a975ba05a9700000000001300025ba05ae25ba05afb00000000001400025ba05b4d5ba05b6600000000001500025ba05bb5ff0202005ba05bd300000000001600025ba05c385ba05c5600000000001700025ba05cab5ba05cc900000000001800025ba05d3c5ba05d5500000000001900025ba05da85ba05dc100000000001a00025ba05e115ba05e2a00000000001b00025ba05eb85ba05ed100000000001c00025ba05f3e5ba05f5700000000001d00025ba05fbe5ba05fd700000000001e00025ba060375ba0605000000000001f00025ba060ae5ba060c700000000002000025ba061a75ba061c500000000002000035ba062005ba062010000000000000003eb001100132000035ba062775ba0627840000000002000015ba062a15ba062bf40000000001f00015ba063835ba0639c40000000001e00015ba063fb5ba0641440000000001d00015ba064745ba0648d40000000001c00015ba064f45ba0650d40000000001b00015ba0656c5ba0658540000000001a00015ba0660d5ba0662640000000001900015ba066755ba0668e40000000001800015ba066e55ba066fe40000000001700015ba0676d5ba0678b40000000001600015ba067e05ba067fe40000000001500015ba068645ba0688240000000001400015ba068d05ba068e940000000001300015ba069415ba0695a40000000001300055ba069ad5ba069ad40000000001200015ba06a005ba06a1940000000001100035ba06a5b5ba06a5b40000000001100025ba06a9d5ba06a9e4000000000";
            in.writeBytes(DataUtil.HexStrToBytes(thirdLocationSecond));

//            String fourLocationSecond = "";
//            in.writeBytes(DataUtil.HexStrToBytes(fourLocationSecond));
          ctx.writeAndFlush(in);
            Thread.sleep(2000);
            System.out.println("send num total= "+num++);
          }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("channelReadchannelRead");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("Service cause Exception "+cause.getMessage());
        ctx.close();
    }
}
